# -*- coding: utf-8 -*-
"""
页面解析器模块
"""

import re
import logging
from datetime import datetime, timedelta
from typing import List, Dict, Any, Optional
from playwright.async_api import Page, Locator


class PageParser:
    """页面解析器"""
    
    def __init__(self):
        self.logger = logging.getLogger(__name__)
    
    async def extract_list_data(self, page: Page) -> List[Dict[str, Any]]:
        """提取列表页数据 - 使用精确的XPath选择器"""
        try:
            # 🔥 修复1: 使用用户提供的精确XPath选择器
            precise_xpath = '/html/body/div[1]/div/div[2]/div[3]/div[1]/ul[1]/li'

            # 首先尝试精确的XPath选择器
            items_locator = None
            try:
                await page.wait_for_selector('xpath=' + precise_xpath, timeout=10000)
                items_locator = page.locator('xpath=' + precise_xpath)
                item_count = await items_locator.count()
                if item_count > 0:
                    self.logger.info(f"✅ 使用精确XPath找到 {item_count} 条数据")
                else:
                    self.logger.warning("精确XPath未找到数据，尝试备用方法")
                    items_locator = None
            except Exception as e:
                self.logger.warning(f"精确XPath失败: {e}，尝试备用方法")
                items_locator = None

            # 备用方法：尝试多种可能的列表选择器
            if not items_locator:
                list_selectors = [
                    'ul li',  # 通用ul li选择器
                    'list',
                    '.list',
                    '[role="list"]',
                    '.item-list',
                    '.data-list',
                    'ul',
                    '.content'
                ]

                # 尝试找到列表容器
                for selector in list_selectors:
                    try:
                        await page.wait_for_selector(selector, timeout=5000)

                        # 尝试不同的列表项选择器
                        item_selectors = [
                            f'{selector} listitem',
                            f'{selector} .item',
                            f'{selector} li',
                            f'{selector} > div',
                            f'{selector} [class*="item"]'
                        ]

                        for item_selector in item_selectors:
                            items = page.locator(item_selector)
                            count = await items.count()
                            if count > 0:
                                items_locator = items
                                self.logger.info(f"使用备用选择器 '{item_selector}' 找到 {count} 条数据")
                                break

                        if items_locator:
                            break

                    except Exception:
                        continue

            # 最后的备用方法：查找包含特定文本的元素
            if not items_locator:
                text_patterns = ['求租行业', '考虑区域', '平米', '租金']
                for pattern in text_patterns:
                    try:
                        elements = page.locator(f'*:has-text("{pattern}")')
                        count = await elements.count()
                        if count > 0:
                            # 找到父级容器
                            parent_elements = elements.locator('xpath=..')
                            parent_count = await parent_elements.count()
                            if parent_count > 0:
                                items_locator = parent_elements
                                self.logger.info(f"通过文本 '{pattern}' 找到 {parent_count} 个父级元素")
                                break
                    except Exception:
                        continue

            if not items_locator:
                self.logger.warning("未找到任何数据列表")
                return []

            item_count = await items_locator.count()
            self.logger.info(f"找到 {item_count} 条数据")

            results = []

            for i in range(min(item_count, 50)):  # 限制最大处理数量
                try:
                    item = items_locator.nth(i)
                    # 🔥 修复: 使用新的精确数据提取方法
                    data = await self._extract_single_item_precise(item)
                    if data:
                        results.append(data)
                except Exception as e:
                    self.logger.error(f"提取第 {i+1} 条数据失败: {e}")
                    continue

            self.logger.info(f"成功提取 {len(results)} 条数据")
            return results

        except Exception as e:
            self.logger.error(f"提取列表数据失败: {e}")
            return []

    async def _extract_single_item_precise(self, item: Locator) -> Optional[Dict[str, Any]]:
        """🔥 新增：精确提取单条数据项 - 修复发布时间和联系人问题"""
        try:
            data = {}

            # 获取整个元素的文本内容用于调试
            full_text = await item.text_content()
            if not full_text:
                return None

            # 清理文本
            clean_text = full_text.replace('\xa0', ' ').replace('\u2000', ' ').strip()

            # 🔥 修复1: 精确提取标题和详情链接
            try:
                # 查找链接元素（标题通常是链接）
                link_element = item.locator('a').first
                if await link_element.count() > 0:
                    title_text = await link_element.text_content()
                    detail_url = await link_element.get_attribute('href')

                    data['title'] = title_text.strip() if title_text else ""
                    data['detail_url'] = self._complete_detail_url(detail_url.strip() if detail_url else "")
                else:
                    # 如果没有链接，从文本中提取标题
                    lines = clean_text.split('\n')
                    data['title'] = lines[0].strip() if lines else ""
                    data['detail_url'] = ""
            except Exception:
                data['title'] = clean_text[:50] + "..." if len(clean_text) > 50 else clean_text
                data['detail_url'] = ""

            # 🔥 修复2: 精确提取发布时间（从列表页获取真实时间）
            publish_date = self._extract_real_publish_time_from_text(clean_text)
            data['publish_date'] = publish_date

            # 🔥 修复3: 简化联系人提取（避免大段文本）
            contact_person = self._extract_simple_contact_from_text(clean_text)
            data['contact_person'] = contact_person

            # 🔥 修复：提取其他字段（修正字段映射错误）
            data['contact_info'] = self._extract_phone_from_text(clean_text)
            data['acceptable_rent'] = self._extract_rent_from_text(clean_text)
            data['building_area'] = self._extract_area_size_from_text(clean_text)  # 修复：使用面积提取而不是区域提取
            data['area_info'] = self._extract_area_from_text(clean_text)  # 新增：区域信息字段

            # 设置默认值
            data['city'] = '深圳'
            data['business_type'] = '餐饮'
            data['data_source'] = 'pupuwang'
            data['crawl_time'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

            return data

        except Exception as e:
            self.logger.error(f"精确提取单条数据失败: {e}")
            return None

    async def _extract_single_item(self, item: Locator) -> Optional[Dict[str, Any]]:
        """提取单条数据项"""
        try:
            data = {}
            
            # 提取标题和详情链接
            title_link = item.locator('link').first
            if await title_link.count() > 0:
                title_text = await title_link.text_content()
                detail_url = await title_link.get_attribute('href')

                data['title'] = title_text.strip() if title_text else ""
                # 完善详情页URL
                data['detail_url'] = self._complete_detail_url(detail_url.strip() if detail_url else "")

                # 从标题中提取联系人信息
                contact_person = self._extract_contact_from_title(title_text)
                data['contact_person'] = contact_person
            
            # 提取业态信息
            business_elements = item.locator('generic:has-text("求租行业：")')
            if await business_elements.count() > 0:
                business_text = await business_elements.first.text_content()
                business_type = self._extract_business_type(business_text)
                data['business_type'] = business_type
            
            # 提取区域信息
            area_elements = item.locator('generic:has-text("考虑区域：")')
            if await area_elements.count() > 0:
                area_text = await area_elements.first.text_content()
                area_info = self._extract_area_info(area_text)
                data['area_info'] = area_info
            
            # 提取时间信息
            time_elements = item.locator('generic img + text')
            if await time_elements.count() > 0:
                time_text = await time_elements.first.text_content()
                publish_time = self._parse_relative_time(time_text)
                # 检查数据是否在一个月内
                if not self._is_data_within_month(publish_time):
                    return None  # 超过一个月的数据视为无效
                data['publish_time'] = publish_time
            
            # 提取面积信息
            size_elements = item.locator('generic:has-text("平米")')
            if await size_elements.count() > 0:
                size_text = await size_elements.first.text_content()
                building_area = self._extract_area_size(size_text)
                data['building_area'] = building_area
            
            # 提取租金信息
            rent_elements = item.locator('generic:has-text("租金")')
            if await rent_elements.count() > 0:
                rent_text = await rent_elements.first.text_content()
                acceptable_rent = self._extract_rent_info(rent_text)
                data['acceptable_rent'] = acceptable_rent
            else:
                # 尝试其他可能的租金表示
                all_text = await item.text_content()
                acceptable_rent = self._extract_rent_from_text(all_text)
                data['acceptable_rent'] = acceptable_rent
            
            # 设置城市（从URL或配置中获取）
            data['city'] = '深圳'
            
            # 设置数据来源
            data['data_source'] = 'pupuwang'
            
            return data

        except Exception as e:
            self.logger.error(f"提取单条数据失败: {e}")
            return None

    async def _extract_single_item_flexible(self, item: Locator) -> Optional[Dict[str, Any]]:
        """灵活提取单条数据项"""
        try:
            data = {}

            # 获取整个元素的文本内容
            full_text = await item.text_content()
            if not full_text:
                return None

            # 清理文本中的特殊字符
            clean_text = full_text.replace('\xa0', ' ').replace('\u2000', ' ').strip()
            self.logger.debug(f"处理文本: {clean_text[:100]}...")

            # 尝试提取联系人（从文本中查找姓名模式）
            contact_person = self._extract_contact_from_text(full_text)
            data['contact_person'] = contact_person

            # 提取业态信息
            business_type = self._extract_business_type_from_text(full_text)
            data['business_type'] = business_type

            # 提取区域信息
            area_info = self._extract_area_from_text(full_text)
            data['area_info'] = area_info

            # 提取面积信息
            building_area = self._extract_area_size_from_text(full_text)
            data['building_area'] = building_area

            # 提取租金信息
            acceptable_rent = self._extract_rent_from_text(full_text)
            data['acceptable_rent'] = acceptable_rent

            # 尝试提取链接
            try:
                links = item.locator('a, link')
                if await links.count() > 0:
                    first_link = links.first
                    detail_url = await first_link.get_attribute('href')
                    title_text = await first_link.text_content()

                    data['detail_url'] = self._complete_detail_url(detail_url if detail_url else "")
                    data['title'] = title_text if title_text else ""
            except Exception:
                data['detail_url'] = ""
                data['title'] = full_text[:50] + "..." if len(full_text) > 50 else full_text

            # 提取时间信息（查找时间模式）
            publish_time = self._extract_time_from_text(full_text)
            # 检查数据是否在一个月内
            if not self._is_data_within_month(publish_time):
                return None  # 超过一个月的数据视为无效
            data['publish_time'] = publish_time

            # 设置默认值
            data['city'] = '深圳'
            data['data_source'] = 'pupuwang'

            # 验证数据有效性
            if not any([data.get('contact_person'), data.get('business_type'), data.get('building_area')]):
                return None

            return data

        except Exception as e:
            self.logger.error(f"灵活提取单条数据失败: {e}")
            return None

    def _extract_contact_from_text(self, text: str) -> str:
        """从文本中提取联系人"""
        if not text:
            return ""

        # 匹配常见的姓名模式
        patterns = [
            r'([王李张刘陈杨黄赵吴周徐孙马朱胡郭何高林罗郑梁谢宋唐许韩冯邓曹彭曾萧田董袁潘于蒋蔡余杜叶程苏魏吕丁任沈姚卢姜崔钟谭陆汪范金石廖贾夏韦付方白邹孟熊秦邱江尹薛闫段雷侯龙史陶黎贺顾毛郝龚邵万钱严覃武戴莫孔向汤][\u4e00-\u9fa5]{0,2}[先生女士])',
            r'([王李张刘陈杨黄赵吴周徐孙马朱胡郭何高林罗郑梁谢宋唐许韩冯邓曹彭曾萧田董袁潘于蒋蔡余杜叶程苏魏吕丁任沈姚卢姜崔钟谭陆汪范金石廖贾夏韦付方白邹孟熊秦邱江尹薛闫段雷侯龙史陶黎贺顾毛郝龚邵万钱严覃武戴莫孔向汤][\u4e00-\u9fa5]{1,2}找)',
            r'([A-Za-z]+[先生女士])',
        ]

        for pattern in patterns:
            match = re.search(pattern, text)
            if match:
                name = match.group(1)
                # 清理姓名
                name = re.sub(r'[先生女士找]', '', name)
                if len(name) >= 1:
                    return name

        return ""

    def _extract_business_type_from_text(self, text: str) -> str:
        """从文本中提取业态类型"""
        business_keywords = {
            '餐饮': ['餐馆', '餐厅', '饭店', '小吃', '快餐', '火锅', '咖啡', '茶饮', '奶茶', '烧烤', '面条', '米饭'],
            '娱乐': ['娱乐', '游戏', 'KTV', '网吧', '台球', '棋牌', '酒吧'],
            '零售': ['零售', '服装', '超市', '便利店', '药店', '化妆品', '数码', '手机']
        }

        for business_type, keywords in business_keywords.items():
            for keyword in keywords:
                if keyword in text:
                    return business_type

        # 默认返回餐饮
        return '餐饮'

    def _extract_area_from_text(self, text: str) -> str:
        """从文本中提取区域信息"""
        # 深圳区域关键词
        areas = ['罗湖区', '福田区', '南山区', '宝安区', '龙岗区', '盐田区', '龙华区', '坪山区', '光明区', '大鹏新区']

        found_areas = []
        for area in areas:
            if area in text:
                found_areas.append(area)

        if found_areas:
            return '、'.join(found_areas)

        # 如果没有找到具体区域，查找"区域"相关文本
        area_match = re.search(r'考虑区域[：:]?\s*([^，。\n]+)', text)
        if area_match:
            return area_match.group(1).strip()

        return ""

    def _extract_area_size_from_text(self, text: str) -> str:
        """从文本中提取面积信息"""
        # 匹配面积模式
        patterns = [
            r'(\d+[-~]\d+)\s*平米?',
            r'(\d+)\s*平米?',
            r'(\d+[-~]\d+)\s*㎡',
            r'(\d+)\s*㎡'
        ]

        for pattern in patterns:
            match = re.search(pattern, text)
            if match:
                return match.group(0)

        return ""

    def _extract_time_from_text(self, text: str) -> datetime:
        """从文本中提取时间信息"""
        now = datetime.now()

        # 查找时间模式
        time_patterns = [
            r'(\d{1,2}:\d{2})',
            r'(\d+分钟前)',
            r'(\d+小时前)',
            r'(昨天)',
            r'(前天)'
        ]

        for pattern in time_patterns:
            match = re.search(pattern, text)
            if match:
                time_text = match.group(1)
                return self._parse_relative_time(time_text)

        return now
    
    async def extract_detail_data(self, page: Page) -> Dict[str, Any]:
        """提取详情页数据"""
        try:
            # 等待页面加载
            await page.wait_for_load_state('networkidle', timeout=15000)

            data = {}

            # 提取联系人信息 - 更精确的选择器
            contact_selectors = [
                'text:has-text("（个人）")',
                'text:has-text("（商家）")',
                'paragraph:has-text("（个人）")',
                'paragraph:has-text("（商家）")',
                'div:has-text("（个人）")',
                'div:has-text("（商家）")'
            ]

            for selector in contact_selectors:
                contact_elements = page.locator(selector)
                if await contact_elements.count() > 0:
                    contact_text = await contact_elements.first.text_content()
                    contact_person = self._extract_contact_person(contact_text)
                    if contact_person:
                        data['contact_person'] = contact_person
                        break

            # 提取联系方式 - 更全面的搜索
            phone_selectors = [
                'text:regex(r"1[3-9]\\d{9}")',  # 直接匹配手机号格式
                'paragraph:has-text("1")',
                'div:has-text("1")',
                'span:has-text("1")'
            ]

            for selector in phone_selectors:
                try:
                    phone_elements = page.locator(selector)
                    for i in range(await phone_elements.count()):
                        phone_text = await phone_elements.nth(i).text_content()
                        if self._is_phone_number(phone_text):
                            data['contact_info'] = self._extract_phone_from_text(phone_text)
                            break
                    if 'contact_info' in data:
                        break
                except Exception:
                    continue

            # 如果还没找到联系方式，尝试从整个页面文本中提取
            if 'contact_info' not in data:
                page_text = await page.text_content()
                phone = self._extract_phone_from_text(page_text)
                if phone:
                    data['contact_info'] = phone
            
            # 提取发布日期
            date_elements = page.locator('generic:has-text("2025"), generic:has-text("2024")')
            if await date_elements.count() > 0:
                date_text = await date_elements.first.text_content()
                publish_date = self._parse_publish_date(date_text)
                data['publish_date'] = publish_date
            
            # 提取详细描述
            desc_elements = page.locator('paragraph, generic')
            desc_texts = []
            for i in range(min(await desc_elements.count(), 10)):  # 限制检查数量
                text = await desc_elements.nth(i).text_content()
                if text and len(text.strip()) > 20:  # 过滤短文本
                    desc_texts.append(text.strip())
            
            if desc_texts:
                data['description'] = ' '.join(desc_texts[:3])  # 取前3段
            
            return data
            
        except Exception as e:
            self.logger.error(f"提取详情页数据失败: {e}")
            return {}
    
    def _extract_contact_from_title(self, title: str) -> str:
        """从标题中提取联系人"""
        if not title:
            return ""
        
        # 匹配常见的姓名模式
        patterns = [
            r'([王李张刘陈杨黄赵吴周徐孙马朱胡郭何高林罗郑梁谢宋唐许韩冯邓曹彭曾萧田董袁潘于蒋蔡余杜叶程苏魏吕丁任沈姚卢姜崔钟谭陆汪范金石廖贾夏韦付方白邹孟熊秦邱江尹薛闫段雷侯龙史陶黎贺顾毛郝龚邵万钱严覃武戴莫孔向汤][\u4e00-\u9fa5]{0,2}[先生女士])',
            r'([A-Za-z]+[先生女士])',
            r'([王李张刘陈杨黄赵吴周徐孙马朱胡郭何高林罗郑梁谢宋唐许韩冯邓曹彭曾萧田董袁潘于蒋蔡余杜叶程苏魏吕丁任沈姚卢姜崔钟谭陆汪范金石廖贾夏韦付方白邹孟熊秦邱江尹薛闫段雷侯龙史陶黎贺顾毛郝龚邵万钱严覃武戴莫孔向汤][\u4e00-\u9fa5]{0,2})',
        ]
        
        for pattern in patterns:
            match = re.search(pattern, title)
            if match:
                return match.group(1)
        
        return ""
    
    def _extract_business_type(self, text: str) -> str:
        """提取业态类型"""
        if not text:
            return ""
        
        # 移除"求租行业："前缀
        business_type = text.replace("求租行业：", "").strip()
        return business_type
    
    def _extract_area_info(self, text: str) -> str:
        """提取区域信息"""
        if not text:
            return ""
        
        # 移除"考虑区域："前缀
        area_info = text.replace("考虑区域：", "").strip()
        return area_info
    
    def _extract_area_size(self, text: str) -> str:
        """提取面积信息"""
        if not text:
            return ""
        
        # 提取数字和平米
        pattern = r'(\d+[-~]\d+|\d+)\s*平米'
        match = re.search(pattern, text)
        if match:
            return match.group(0)
        
        return text.strip()
    
    def _extract_rent_info(self, text: str) -> str:
        """提取租金信息"""
        if not text:
            return ""
        
        # 清理文本
        rent_info = text.strip()
        
        # 移除可能的前缀
        rent_info = re.sub(r'^租金[：:]?\s*', '', rent_info)
        
        return rent_info
    
    def _extract_rent_from_text(self, text: str) -> str:
        """从文本中提取租金信息"""
        if not text:
            return ""

        # 匹配租金模式，按优先级排序
        patterns = [
            r'(\d+[-~–—]\d+元/月)',  # 范围租金，如"6000-8000元/月"
            r'(\d+[-~–—]\d+万/月)',  # 范围租金（万），如"1-2万/月"
            r'(\d+元/月)',          # 固定租金，如"5000元/月"
            r'(\d+万/月)',          # 固定租金（万），如"1万/月"
            r'(面议)',              # 面议
            r'(租金面议)',          # 租金面议
            r'(\d+[-~–—]\d+)',      # 纯数字范围，如"6000-8000"
            r'(\d+)',               # 纯数字，如"5000"
        ]

        for pattern in patterns:
            match = re.search(pattern, text)
            if match:
                rent_str = match.group(1)
                # 标准化租金格式
                return self._standardize_rent_format(rent_str)

        return "面议"

    def _standardize_rent_format(self, rent_str: str) -> str:
        """标准化租金格式"""
        if not rent_str:
            return "面议"

        # 如果已经包含"面议"，直接返回
        if "面议" in rent_str:
            return "面议"

        # 如果包含元/月，直接返回
        if "元/月" in rent_str:
            return rent_str

        # 如果包含万/月，直接返回
        if "万/月" in rent_str:
            return rent_str

        # 如果是纯数字或数字范围，添加元/月
        if re.match(r'^\d+[-~–—]\d+$', rent_str):
            return f"{rent_str}元/月"
        elif re.match(r'^\d+$', rent_str):
            return f"{rent_str}元/月"

        return rent_str
    
    def _parse_relative_time(self, time_text: str) -> datetime:
        """解析相对时间"""
        if not time_text:
            return datetime.now()

        now = datetime.now()
        time_text = time_text.strip()

        # 处理相对时间
        if "分钟前" in time_text:
            minutes = re.search(r'(\d+)分钟前', time_text)
            if minutes:
                return now - timedelta(minutes=int(minutes.group(1)))
        elif "小时前" in time_text:
            hours = re.search(r'(\d+)小时前', time_text)
            if hours:
                return now - timedelta(hours=int(hours.group(1)))
        elif "昨天" in time_text:
            return now - timedelta(days=1)
        elif "前天" in time_text:
            return now - timedelta(days=2)
        elif re.match(r'\d{2}:\d{2}', time_text):
            # 今天的时间，如 "07:15"
            time_parts = time_text.split(':')
            return now.replace(hour=int(time_parts[0]), minute=int(time_parts[1]), second=0, microsecond=0)
        elif re.match(r'\d{2}-\d{2}', time_text):
            # 月日格式，如 "07-16"
            month_day = time_text.split('-')
            month = int(month_day[0])
            day = int(month_day[1])

            # 构造今年的日期
            target_date = now.replace(month=month, day=day, hour=0, minute=0, second=0, microsecond=0)

            # 如果日期在未来，说明是去年的
            if target_date > now:
                target_date = target_date.replace(year=now.year - 1)

            return target_date
        elif re.match(r'\d{4}-\d{2}-\d{2}', time_text):
            # 完整日期格式，如 "2025-07-16"
            try:
                return datetime.strptime(time_text[:10], '%Y-%m-%d')
            except ValueError:
                pass

        return now

    def _is_data_within_month(self, publish_time: datetime) -> bool:
        """检查数据是否在一个月内"""
        now = datetime.now()
        one_month_ago = now - timedelta(days=30)
        return publish_time >= one_month_ago

    def _complete_detail_url(self, url: str) -> str:
        """完善详情页URL，添加域名前缀"""
        if not url:
            return ""

        # 如果已经是完整URL，直接返回
        if url.startswith('http'):
            return url

        # 如果是相对路径，添加域名前缀
        if url.startswith('/'):
            return f"https://sz.pupuwang.com{url}"

        return url
    
    def _extract_contact_person(self, text: str) -> str:
        """🔥 修复：提取联系人姓名（避免大段文本）"""
        if not text:
            return ""

        # 🔥 修复：限制文本长度，避免处理大段描述
        if len(text) > 200:
            # 如果文本太长，只取前200个字符进行处理
            text = text[:200]

        # 移除（个人）或（商家）标识
        clean_text = re.sub(r'[（(][^）)]*[）)]', '', text).strip()

        # 🔥 修复：使用简化的联系人提取逻辑
        contact_name = self._extract_simple_contact_from_text(clean_text)

        # 如果没有找到标准格式的联系人，尝试从清理后的文本中提取
        if not contact_name and clean_text:
            # 只返回前20个字符，避免大段文本
            if len(clean_text) <= 20:
                return clean_text
            else:
                # 如果还是太长，返回空字符串
                return ""

        return contact_name
    
    def _is_phone_number(self, text: str) -> bool:
        """判断是否为电话号码"""
        if not text:
            return False

        # 匹配手机号码模式
        phone_pattern = r'1[3-9]\d{9}'
        # 移除所有非数字字符
        digits_only = re.sub(r'\D', '', text)

        return bool(re.search(phone_pattern, digits_only))

    def _extract_phone_from_text(self, text: str) -> str:
        """从文本中提取电话号码"""
        if not text:
            return ""

        # 匹配手机号码模式
        phone_pattern = r'1[3-9]\d{9}'
        match = re.search(phone_pattern, text)

        if match:
            return match.group(0)

        return ""
    
    def _parse_publish_date(self, text: str) -> datetime:
        """解析发布日期"""
        if not text:
            return datetime.now()
        
        # 匹配日期时间模式
        patterns = [
            r'(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})',
            r'(\d{4}/\d{2}/\d{2}\s+\d{2}:\d{2}:\d{2})',
            r'(\d{4}-\d{2}-\d{2})',
            r'(\d{4}/\d{2}/\d{2})'
        ]
        
        for pattern in patterns:
            match = re.search(pattern, text)
            if match:
                try:
                    date_str = match.group(1)
                    if '-' in date_str:
                        if ':' in date_str:
                            return datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
                        else:
                            return datetime.strptime(date_str, '%Y-%m-%d')
                    else:
                        if ':' in date_str:
                            return datetime.strptime(date_str, '%Y/%m/%d %H:%M:%S')
                        else:
                            return datetime.strptime(date_str, '%Y/%m/%d')
                except ValueError:
                    continue
        
        return datetime.now()

    def _extract_real_publish_time_from_text(self, text: str) -> str:
        """🔥 新增：从文本中提取真实发布时间（修复发布时间字段）"""
        if not text:
            return datetime.now().strftime('%Y-%m-%d %H:%M:%S')

        # 根据用户提供的时间格式进行匹配
        time_patterns = [
            # 格式1: HH:MM (当天时间，如 "06:52")
            (r'\b(\d{2}:\d{2})\b', 'today_time'),
            # 格式2: 昨天
            (r'昨天', 'yesterday'),
            # 格式3: MM-DD (月-日格式，如 "07-17")
            (r'\b(\d{2}-\d{2})\b', 'month_day'),
            # 格式4: 相对时间
            (r'(\d+)分钟前', 'minutes_ago'),
            (r'(\d+)小时前', 'hours_ago'),
            (r'(\d+)天前', 'days_ago'),
        ]

        for pattern, time_type in time_patterns:
            match = re.search(pattern, text)
            if match:
                if time_type == 'today_time':
                    # 今天的具体时间
                    time_str = match.group(1)
                    try:
                        hour, minute = map(int, time_str.split(':'))
                        today = datetime.now().replace(hour=hour, minute=minute, second=0, microsecond=0)
                        return today.strftime('%Y-%m-%d %H:%M:%S')
                    except:
                        pass

                elif time_type == 'yesterday':
                    # 昨天
                    yesterday = datetime.now() - timedelta(days=1)
                    return yesterday.strftime('%Y-%m-%d %H:%M:%S')

                elif time_type == 'month_day':
                    # MM-DD格式
                    month_day = match.group(1)
                    try:
                        month, day = map(int, month_day.split('-'))
                        current_year = datetime.now().year
                        target_date = datetime(current_year, month, day)

                        # 如果日期在未来，说明是去年的
                        if target_date > datetime.now():
                            target_date = datetime(current_year - 1, month, day)

                        return target_date.strftime('%Y-%m-%d %H:%M:%S')
                    except:
                        pass

                elif time_type == 'minutes_ago':
                    # N分钟前
                    minutes = int(match.group(1))
                    past_time = datetime.now() - timedelta(minutes=minutes)
                    return past_time.strftime('%Y-%m-%d %H:%M:%S')

                elif time_type == 'hours_ago':
                    # N小时前
                    hours = int(match.group(1))
                    past_time = datetime.now() - timedelta(hours=hours)
                    return past_time.strftime('%Y-%m-%d %H:%M:%S')

                elif time_type == 'days_ago':
                    # N天前
                    days = int(match.group(1))
                    past_time = datetime.now() - timedelta(days=days)
                    return past_time.strftime('%Y-%m-%d %H:%M:%S')

        # 如果没有匹配到任何时间格式，返回当前时间
        return datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    def _extract_simple_contact_from_text(self, text: str) -> str:
        """🔥 新增：简化联系人提取（修复联系人字段问题）"""
        if not text:
            return ""

        # 只提取简洁的联系人姓名，避免大段描述文本
        contact_patterns = [
            # 匹配 "姓名+先生/女士/生" 格式
            r'([王李张刘陈杨黄赵吴周徐孙马朱胡郭何高林罗郑梁谢宋唐许韩冯邓曹彭曾萧田董袁潘于蒋蔡余杜叶程苏魏吕丁任沈姚卢姜崔钟谭陆汪范金石廖贾夏韦付方白邹孟熊秦邱江尹薛闫段雷侯龙史陶黎贺顾毛郝龚邵万钱严覃武戴莫孔向汤][\u4e00-\u9fa5]{0,2}[先生女士生])',
            # 匹配单个姓氏+生 (如"李生")
            r'([王李张刘陈杨黄赵吴周徐孙马朱胡郭何高林罗郑梁谢宋唐许韩冯邓曹彭曾萧田董袁潘于蒋蔡余杜叶程苏魏吕丁任沈姚卢姜崔钟谭陆汪范金石廖贾夏韦付方白邹孟熊秦邱江尹薛闫段雷侯龙史陶黎贺顾毛郝龚邵万钱严覃武戴莫孔向汤]生)',
            # 匹配英文名+先生/女士
            r'([A-Za-z]+[先生女士])',
        ]

        for pattern in contact_patterns:
            match = re.search(pattern, text)
            if match:
                contact_name = match.group(1)
                # 确保联系人姓名不超过10个字符（避免提取到长文本）
                if len(contact_name) <= 10:
                    return contact_name

        # 如果没有找到标准格式，返回空字符串而不是大段文本
        return ""
